function restore() {
	$("#urls").append(function () {
		var content = "";
		JSON.parse(localStorage["urls"] || "[]").forEach(function (url) {
			content += createInputElement(url);
		});
		return content || createInputElement();
	});
}

function save() {
	var urls = [];
	$("[name='url']").each(function (index) {
		if (this.value && this.value.match(/^[a-zA-Z0-9]+$/)) {
			urls.push(this.value);
		}
	});
	localStorage["urls"] = JSON.stringify(urls);
}

function createInputElement(value) {
	return "<input type='text' name='url' value='" + (value || "") + "'><br>"
}

$(function () {
	restore();
	$("#addButton").click(function () {
		$("#urls").append(createInputElement()).find("input:last").focus();
	});
	$("#saveButton").click(function () {
		save();
		window.close();
	});
	$("#urls").find("input:first").focus();
});